package com.wall.haven.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wall.haven.entity.SystemConfig;
import com.wall.haven.enums.ConfigKey;
import com.wall.haven.mapper.SystemConfigMapper;
import com.wall.haven.service.ISystemConfigService;
import com.wall.haven.util.RSAUtils;
import com.wall.haven.util.UUIDGenerator;
import org.springframework.stereotype.Service;

import java.security.KeyPair;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ChenWangKun
 * @since 2023-10-18
 */
@Service
public class SystemConfigServiceImpl extends ServiceImpl<SystemConfigMapper, SystemConfig> implements ISystemConfigService {


    @Override
    public void clear() {
        this.remove(Wrappers.<SystemConfig>lambdaQuery().eq(SystemConfig::getConfigKey, ConfigKey.SECRET_KEY.name()));
        this.remove(Wrappers.<SystemConfig>lambdaQuery().eq(SystemConfig::getConfigKey, ConfigKey.RSA_PUBLIC_KEY.name()));
        this.remove(Wrappers.<SystemConfig>lambdaQuery().eq(SystemConfig::getConfigKey, ConfigKey.RSA_PRIVATE_KEY.name()));
        this.remove(Wrappers.<SystemConfig>lambdaQuery().eq(SystemConfig::getConfigKey, ConfigKey.AES_KEY.name()));
    }

    @Override
    public void init() throws Exception {
        if (this.getSecretKey() == null){
            this.save(new SystemConfig(ConfigKey.SECRET_KEY.name(), UUIDGenerator.generateUUID(16)));
        }
        if (this.getSecretValidTime() == 0L){
            this.save(new SystemConfig(ConfigKey.SECRET_VALID_TIME.name(), "3600_000"));
        }
        if (this.getRsaPrivateKey() == null || this.getRsaPublicKey() == null){
            KeyPair keyPair = RSAUtils.generateKeyPair();
            this.save(new SystemConfig(ConfigKey.RSA_PUBLIC_KEY.name(), RSAUtils.keyToString(keyPair.getPublic())));
            this.save(new SystemConfig(ConfigKey.RSA_PRIVATE_KEY.name(), RSAUtils.keyToString(keyPair.getPrivate())));
        }
        if (this.getAesKey() == null){
            this.save(new SystemConfig(ConfigKey.AES_KEY.name(), UUIDGenerator.generateUUID(16)));
        }
        if (this.getValue(ConfigKey.SHOW_SECRET_KEY) == null){
            this.save(new SystemConfig(ConfigKey.SHOW_SECRET_KEY.name(), "true"));
        }
    }

    @Override
    public String getValue(ConfigKey configKey) {
        SystemConfig one = this.getOne(Wrappers.<SystemConfig>lambdaQuery().eq(SystemConfig::getConfigKey, configKey.name()));
        if (one != null) {
            return one.getConfigValue();
        }
        return null;
    }

    @Override
    public String getSecretKey() {
        return getValue(ConfigKey.SECRET_KEY);
    }

    @Override
    public boolean isShowSecretKey() {
        String value = getValue(ConfigKey.SHOW_SECRET_KEY);
        return Boolean.parseBoolean(value);
    }

    @Override
    public long getSecretValidTime() {
        String value = getValue(ConfigKey.SECRET_VALID_TIME);
        return value == null ? 0L : Long.parseLong(value);
    }

    @Override
    public String getDefaultAvatar() {
        return getValue(ConfigKey.DEFAULT_AVATAR);
    }

    @Override
    public String getRsaPublicKey() {
        return getValue(ConfigKey.RSA_PUBLIC_KEY);
    }

    @Override
    public String getRsaPrivateKey() {
        return getValue(ConfigKey.RSA_PRIVATE_KEY);
    }

    @Override
    public String getAesKey() {
        return getValue(ConfigKey.AES_KEY);
    }

    @Override
    public boolean isNsfw() {
        String value = getValue(ConfigKey.NSFW);
        return Boolean.parseBoolean(value);
    }
}
